import numpy as np
import matplotlib.pyplot as plt
import control as ctrl

# Step 1: Define a simple second-order plant (G(s) = 1 / (s^2 + 2s + 1))
numerator = [1]
denominator = [1, 2, 1]
plant = ctrl.TransferFunction(numerator, denominator)

# Step 2: Plot the Bode plot of the plant
plt.figure()
ctrl.bode_plot(plant, dB=True)
plt.show()

# Step 3: Design a PID controller (Kp, Ki, Kd)
Kp = 1.0
Ki = 0.5
Kd = 0.1
pid_controller = ctrl.TransferFunction([Kd, Kp, Ki], [1, 0])

# Step 4: Combine the plant and the controller (G(s) * C(s))
open_loop = plant * pid_controller

# Step 5: Plot the Bode plot of the combined system
plt.figure()
ctrl.bode_plot(open_loop, dB=True)
plt.show()

# You can fine-tune Kp, Ki, and Kd based on the Bode plot to achieve desired results.
